package cn.zzq.shop.head.springmvc.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.zzq.shop.common.controller.BaseController;
import cn.zzq.shop.common.util.ConstatFinalUtil;
import cn.zzq.shop.common.util.PageInfoUtil;
import cn.zzq.shop.goods.pojo.AGoods;
import cn.zzq.shop.goods.pojo.AGoodsCate;
import cn.zzq.shop.goods.pojo.AGoodsEnum;
import cn.zzq.shop.goods.service.IGoodsService;
import cn.zzq.shop.goods.service.ISpliderDataService;

@Controller
public class IndexHeadController extends BaseController
{
	@Resource
	private IGoodsService goodsService;
	@Resource
	private ISpliderDataService spliderDataService;
	/*打开首页*/
	@RequestMapping("/index")
	private String index(){
		ConstatFinalUtil.SYS_LOGGER.info("----index----");
		return "/head/index";
	}
	
	/*商品列表*/
	@RequestMapping("/cate_{cateUrl}")
	public String goodsCateList(@PathVariable String cateUrl, Model model,HttpServletRequest request){
		ConstatFinalUtil.SYS_LOGGER.info("----goodsCateList----");
		String keyword = request.getParameter("keyword");
		/*处理get请求乱码*/
		/*if(keyword != null && !"".equalsIgnoreCase(keyword)){
			if("get".equalsIgnoreCase(request.getMethod())){	
				try
				{
					keyword = new String(keyword.getBytes("ISO8859-1"), "UTF-8");
					//keyword = new String(keyword.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e)
				{
					e.printStackTrace();
				}
			}
		}*/
		return this.goodsList("1", keyword,cateUrl, model);
	}
	
	/*商品列表*/
	@RequestMapping("/goods_{cateUrl}_{currentPage}")
	public String goodsCateList(@PathVariable String cateUrl, @PathVariable String currentPage,HttpServletRequest request, Model model){
		ConstatFinalUtil.SYS_LOGGER.info("-----goodsCateList-----");
		String keyword = request.getParameter("keyword");
		/*处理get请求乱码*/
		/*if(keyword != null && !"".equalsIgnoreCase(keyword)){
			if("get".equalsIgnoreCase(request.getMethod())){	
				try
				{
					keyword = new String(keyword.getBytes("ISO8859-1"), "UTF-8");
					//keyword = new String(keyword.getBytes("ISO8859-1"),"UTF-8");
				} catch (UnsupportedEncodingException e)
				{
					e.printStackTrace();
				}
			}
		}*/
		return this.goodsList(currentPage,keyword, cateUrl, model);
	}
	
	/*抽取的方法*/
	private String goodsList(String currentPage,String keyword,String cateUrl,Model model){
		
		/*查询商品分类*/
		Map<String, Object> condMap = new HashMap<>();
		condMap.put("url", cateUrl);
		AGoodsCate goodsCate = this.goodsService.findOneGoodsCateService(condMap);
		Map<String, Object> resultMap = new HashMap<>();
		
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(currentPage, "16");
		/*根据商品分类的id和启用状态查询商品,模糊查询用solr，非模糊查询查数据库*/
		if(keyword != null && !"".equalsIgnoreCase(keyword)){
			condMap.put("keyword", keyword);
			resultMap = this.spliderDataService.findCondListGoodsService(pageInfoUtil, condMap);
		}else{
			
			condMap.clear();
			condMap.put("cateId", goodsCate.getId());
			condMap.put("status", AGoodsEnum.STATUS_ENABLE.getStatus());
			resultMap = this.goodsService.findCondListGoodsService(pageInfoUtil, condMap);
		}
		List<AGoods> goodList = (List<AGoods>) resultMap.get("list");
		int totalRecord = Integer.valueOf(resultMap.get("totalRecord") + "");
		pageInfoUtil.setTotalRecord(totalRecord);
		/*存储结果*/
		model.addAttribute("goodsCate",goodsCate);
		model.addAttribute("goodsList",goodList);
		model.addAttribute("pageInfoUtil",pageInfoUtil);
		model.addAttribute("keyword",keyword);
		return "/head/goodsList";
	}
	
}
